package com.zhongxi.guoyuan.OffLineShop;


import android.app.AlertDialog;
import android.content.Intent;
import android.os.CountDownTimer;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhongxi.guoyuan.OffLineShop.Model.StoreDetail;
import com.zhongxi.guoyuan.OffLineShop.Model.StoreMessage;
import com.zhongxi.guoyuan.OffLineShop.UI.SurePayActivity;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.CommonTool;
import com.zhongxi.guoyuan.utils.DataUtil;
import com.zhongxi.guoyuan.utils.ToastUtil;
import com.zhongxi.guoyuan.utils.UiUtils;
import com.zhongxi.guoyuan.view.HtmlActivity;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class ToPayFragment extends OffLineBaseFragment {


    @Bind(R.id.iv_image)
    ImageView mIvImage;
    @Bind(R.id.tv_storeName)
    TextView mTvStoreName;
    @Bind(R.id.tv_incentive)
    TextView mTvIncentive;
    @Bind(R.id.tv_distance)
    TextView mTvDistance;
    @Bind(R.id.et_pay_money)
    EditText mEtPayMoney;
    @Bind(R.id.tv_flag_dou)
    TextView mTvFlagDou;
    @Bind(R.id.declare)
    TextView mDeclare;
    @Bind(R.id.et_notes)
    EditText mEtNotes;
    @Bind(R.id.btn_pay)
    Button mBtnPay;
    private StoreDetail mStoreDetail;

    public ToPayFragment() {
        // Required empty public constructor
    }

    @Override
    protected void initParams() {
        super.initParams();
        if (getArguments() != null) {
            mStoreDetail = (StoreDetail) getArguments().getSerializable("storeDetail");
        }
    }

    @Override
    protected View init() {
        View mView = UiUtils.inflate(R.layout.fragment_to_pay);
        ButterKnife.bind(this, mView);
        return mView;
    }


    @Override
    protected void initView(View view) {
        super.initView(view);

        showDialog();

        StoreMessage mData = mStoreDetail.getData();
        if (mData.getThumbnail().endsWith(".jpg") || mData.getThumbnail().endsWith(".png")) {
            Glide.with(UiUtils.getContext()).load(Url.IMG_URL + mData.getThumbnail()).placeholder(R.mipmap.iv_loading).into(mIvImage);
        } else {
            Glide.with(UiUtils.getContext()).load(Url.IMG_URL + mData.getImages().get(0)).placeholder(R.mipmap.iv_loading).into(mIvImage);
        }
        mTvStoreName.setText(mData.getName());

        String mDistance = CommonTool.getDistanceKM(Double.parseDouble(MyApplication.getInstance().getLongitude()),
                Double.parseDouble(MyApplication.getInstance().getLatitude()), mData.getLongitude(), mData.getDimensions());
        if (DataUtil.moreThanOther(mDistance, "1")) {
            mTvDistance.setText(getString(R.string.distance, mDistance) + "km");
        } else {
            mTvDistance.setText(getString(R.string.distance, CommonTool.getDistanceOfM(Double.parseDouble(MyApplication.getInstance().getLongitude()),
                    Double.parseDouble(MyApplication.getInstance().getLatitude()), mData.getLongitude(), mData.getDimensions())) + "m");
        }

    }

    @Override
    protected void initListener() {
        super.initListener();
        mEtPayMoney.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence.length() > 0) {
                    mTvFlagDou.setText(CommonTool.getWhole(charSequence.toString()) + " 美豆");
                } else {
                    mTvFlagDou.setText("0 美豆");
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });
        mEtNotes.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 20) {
                    ToastUtil.show("抱歉!备注最多20个字。");
                }
            }
        });
    }

    @OnClick({R.id.declare, R.id.btn_pay})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.declare:
                Intent intent = new Intent(UiUtils.getContext(), HtmlActivity.class);
                intent.putExtra("type", "doudouRule");
                startActivity(intent);
                break;
            case R.id.btn_pay:
                String mTrim = mEtPayMoney.getText().toString().trim();
                if (TextUtils.isEmpty(mTrim)) {
                    ToastUtil.show(getString(R.string.et_declare));
                } else if (mTrim.contains(".") && ((mTrim.substring(mTrim.indexOf(".") + 1, mTrim.length()).length() > 2) || mTrim.indexOf(".") == 0)) {
                    ToastUtil.show(getString(R.string.error_money));
                } else {
                    Intent mIntent = new Intent(UiUtils.getContext(), SurePayActivity.class);
                    StoreMessage mData = mStoreDetail.getData();
                    mIntent.putExtra("name", mData.getName());
                    mIntent.putExtra("longitude", mData.getLongitude());
                    mIntent.putExtra("dimensions", mData.getDimensions());
                    mIntent.putExtra("money", mTrim);
                    mIntent.putExtra("notes", mEtNotes.getText().toString().trim());
                    mIntent.putExtra("paysecret", mStoreDetail.getPaysecret());
                    mIntent.putExtra("shopid", mData.getId());
                    startActivity(mIntent);
                }
                break;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


    private AlertDialog mDialog;

    CountDownTimer mTimer = new CountDownTimer(3000, 1000) {
        @Override
        public void onTick(long l) {

        }

        @Override
        public void onFinish() {
            mDialog.dismiss();
        }
    };

    private void showDialog() {
        mTimer.start();
        mDialog = new AlertDialog.Builder(getActivity()).create();
        View view = UiUtils.inflate(R.layout.dialog_offline);
        ImageView mImageView = (ImageView) view.findViewById(R.id.iv_Cancel);
        TextView mTextView = (TextView) view.findViewById(R.id.tv_distance);
        String mDistance = CommonTool.getDistanceKM(Double.parseDouble(MyApplication.getInstance().getLongitude()),
                Double.parseDouble(MyApplication.getInstance().getLatitude()), mStoreDetail.getData()
                        .getLongitude(), mStoreDetail.getData().getDimensions());
        if (DataUtil.moreThanOther(mDistance, "1")) {
            mTextView.setText(getString(R.string.store_distance, mDistance) + "km");
        } else {
            mTextView.setText(getString(R.string.store_distance, CommonTool.getDistanceOfM(Double.parseDouble(MyApplication.getInstance().getLongitude()),
                    Double.parseDouble(MyApplication.getInstance().getLatitude()), mStoreDetail.getData()
                            .getLongitude(), mStoreDetail.getData().getDimensions())) + "m");
        }
        mImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mDialog.dismiss();
                mTimer.onFinish();
                mTimer.cancel();
            }
        });
        mDialog.setView(view);
        mDialog.show();
    }

}
